package cn.tedu.loding.service.impl;

import cn.tedu.loding.ex.ServiceException;
import cn.tedu.loding.mapper.AttributeMapper;
import cn.tedu.loding.mapper.AttributeTemplateMapper;
import cn.tedu.loding.pojo.dto.AttributeAddNewDTO;
import cn.tedu.loding.pojo.dto.AttributeUpdateDTO;
import cn.tedu.loding.pojo.entity.Attribute;
import cn.tedu.loding.pojo.vo.*;
import cn.tedu.loding.pojo.vo.AttributeStandardVO;
import cn.tedu.loding.rope.iml.AttributeRedisRepositoryImpl;
import cn.tedu.loding.service.IAttributeService;
import cn.tedu.loding.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class AttributeServiceImpl implements IAttributeService {
    @Autowired
    AttributeMapper attributeMapper;
    @Autowired
    AttributeTemplateMapper attributeTemplateMapper;
    @Autowired
    AttributeRedisRepositoryImpl attributeRedisRepository;


//    添加属性
    @Override
    public void addNew(AttributeAddNewDTO attributeAddNewDTO) {
        log.debug("开始处理【添加属性】业务,参数为{}",attributeAddNewDTO);
        {
            String name = attributeAddNewDTO.getName();
            int count = attributeMapper.countByName(name);
            if (count > 0) {
                String message = "属性名已被占用,添加失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }
        {
            log.debug("将根据属性模板ID【{}】查询该属性的属性模板ID是否存在】",attributeAddNewDTO.getTemplateId());
            AttributeTemplateStandardVO Query = attributeTemplateMapper.getStandardById(attributeAddNewDTO.getTemplateId());
            if (Query==null){
                String message = "所选的属性模板不存在,添加失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewDTO,attribute);
        attribute.setGmtCreate(LocalDateTime.now());
        attribute.setGmtModified(LocalDateTime.now());
        log.debug("名称未占用,将插入数据{}",attribute);
        int count = attributeMapper.insert(attribute);
        if (count!=1){
            String message = "服务器繁忙,稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        log.debug("插入成功,数据为{}",attribute);
    }

//    删除属性
    @Override
    public void delete(Long id) {
        log.debug("开始根据id【{}】处理删除属性操作",id);
        {
            AttributeStandardVO result= attributeMapper.getStandardById(id);
            if (result == null) {
                String message = "属性不存在,删除失败";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }
        log.debug("将进行删除操作");
            int count = attributeMapper.deleteById(id);
            if (count != 1) {
                String message = "服务器繁忙,稍后再试";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
            log.debug("删除成功!");
    }

    //根据id修改属性
    @Override
    public void update(AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理【根据id修改属性】");
        AttributeStandardVO attributeStandardVO = attributeMapper.getStandardById(attributeUpdateDTO.getId());
        if (attributeStandardVO==null){
            String message = "修改失败,id无效";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        {
            Long Tid = attributeUpdateDTO.getTemplateId();
            int count = attributeTemplateMapper.countById(Tid);
            log.debug("根据属性模板id【{}】,count:{}",Tid,count);
            if (count == 0){
                String message = "修改属性失败,所选择的属性模板不存在";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
            }
        }
        {
            int count = attributeMapper.countByNameAndTemplateId(attributeUpdateDTO.getName(),attributeUpdateDTO.getTemplateId());
            if (count > 0) {
                String message = "修改失败,所选属性模板下该属性名已存在";
                log.debug(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeUpdateDTO,attribute);
        attribute.setGmtModified(LocalDateTime.now());
        log.debug("将执行修改操作");
        int count = attributeMapper.update(attribute);
        if (count!=1){
            String message = "修改失败,服务器繁忙,稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
    }

    //根据id查询属性详情
    @Override
    public AttributeStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询属性详情】业务,参数为:{}",id);
        log.debug("将从Redis中获取相关数据");
        AttributeStandardVO attributeStandardVO = attributeMapper.getStandardById(id);
        if (attributeStandardVO==null){
            String message = "获取属性详情失败,尝试访问的数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //将查询结果写入到缓存,并返回
        log.debug("从数据库中查询到的有效结果{}",attributeStandardVO);
        return attributeStandardVO;
    }

//    属性列表
    @Override
    public List<AttributeListVO> list() {
        log.debug("开始处理【属性列表】业务,无参数");
        return attributeMapper.list();
    }


}
